package br.com.fotoxbh.geracao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

import sun.print.Win32MediaTray;
import br.gov.anatel.dcomtrol.win.ShellIntegration;

import com.fotoloka.util.ParamUtil;
import com.fotoloka.view.Janela;


public class Impressao {

	public static void main(String[] args) throws FileNotFoundException, PrintException, IOException {
		File foto = new File("C:\\Users\\pablo.reis\\FOTOX\\FORMATADAS\\secao_22.jpg");
		
		imprimir(foto);
	}

	public static void imprimir(File foto) throws FileNotFoundException,
			PrintException, IOException {
		
		boolean apresentarImagem = false;
		
		if(apresentarImagem) {
		
			ShellIntegration.exec(foto.getAbsolutePath());
		
		} else {
		
			
			PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			pras.add(new Copies(1));
			//pras.add(new MediaPrintableArea(0.0f, 0.0f, 101.6f, 152.4f, MediaPrintableArea.MM));
			//pras.add(new MediaPrintableArea(0,0, 4, 6,MediaPrintableArea.INCH));
			pras.add(MediaSizeName.ISO_A6);
			pras.add(OrientationRequested.LANDSCAPE);
			//pras.add(new PrinterResolution(96, 96, ResolutionSyntax.DPI));
			
			
			PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.JPEG, null);
			if (pss.length == 0)
			  throw new RuntimeException("No printer services available.");
			
			PrintService ps = null;
			for(PrintService print : pss) {
				if(print.getName().contains(ParamUtil.getImpressora())) {
					ps = print;
				}
			}
			if(ps == null) {
				Logger.getLogger(Janela.class.toString()).log(Level.SEVERE, "Impressora não conectada");
				return;
			}
			ps.getSupportedAttributeCategories();
			
			Logger.getLogger(Janela.class.toString()).log(Level.INFO, "Imprimindo em " + ps);
			DocPrintJob job = ps.createPrintJob();
			
			FileInputStream fin = new FileInputStream(foto);
			DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; 
			Doc doc = new SimpleDoc(fin, flavor, null); 
			job.print(doc, pras);
			fin.close();
		}
	}
}
